package server;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import util.Message;
import util.Status;
import data.DataService;
import dataPO.InfoPO;

public class Connector {

	private static String IP = "localhost";
	private static int PORT = 8889;

	private static DataService getRefObject() {
		try {
			return (DataService) Naming.lookup("rmi://" + IP + ":" + PORT + "/data");

		} catch (NotBoundException | MalformedURLException | RemoteException e) {
		    return null;
		}		
	}

	public static Message add(InfoPO info) {
		try {
		    
		    return getRefObject().add(info);

		} catch (RemoteException | NullPointerException e) {
		    return new Message(Status.NET_CONNECTION_FAILED);
		}
	}

	public static Message delete(InfoPO info) {
	    try {
            return getRefObject().delete(info);

        } catch (RemoteException | NullPointerException e) {
            return new Message(Status.NET_CONNECTION_FAILED);
        }
	}

	public static Message update(InfoPO info) {
	    try {
            return getRefObject().update(info);

        } catch (RemoteException | NullPointerException e) {
            return new Message(Status.NET_CONNECTION_FAILED);
        }
	}

	public static Message getCourseRecord(String userID) {
	    try {
            return getRefObject().getCoursesRecord(userID);

        } catch (RemoteException | NullPointerException e) {
            return new Message(Status.NET_CONNECTION_FAILED);
        }
	}

	public static Message getCourseStudentRecord(String courseID) {
	    try {
	        System.out.println("Connector called");
            return getRefObject().getCourseStudentRecord(courseID);

        } catch (RemoteException | NullPointerException e) {
            return new Message(Status.NET_CONNECTION_FAILED);
        }
	}

	public static Message getAccountInfo(String userID) {
	    try {
           return getRefObject().getAccountInfo(userID);

        } catch (RemoteException | NullPointerException e) {
            return new Message(Status.NET_CONNECTION_FAILED);
        }
	}

	public static Message getCourseInfo(String... content) {
	    try {
            return getRefObject().getCourseInfo(content);

        } catch (RemoteException | NullPointerException e) {
            return new Message(Status.NET_CONNECTION_FAILED);
        }
	}

	public static Message getUsersBy(String... attr) {
	    try {
            return getRefObject().getUsersBy(attr);

        } catch (RemoteException | NullPointerException e) {
            return new Message(Status.NET_CONNECTION_FAILED);
        }
	}

	public static Message getTeachPlan(String department) {
	    try {
            return getRefObject().getTeachPlan(department);

        } catch (RemoteException | NullPointerException e) {
            return new Message(Status.NET_CONNECTION_FAILED);
        }
	}

	public static Message getTeachFrame() {
	    try {
           return getRefObject().getTeachFrame();

        } catch (RemoteException | NullPointerException e) {
            return new Message(Status.NET_CONNECTION_FAILED);
        }
	}
	
}
